<template>
	<view >
		<view class="my-user-info">
			<view class="top-box">
				<button class="avatar" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
					<image :src="avatarUrl" class="avatar-img"></image>
				</button>
				<input class="nickname" type="nickname" :value="nickname" @blur="bindblur" placeholder="请输入昵称" />
			</view>
		</view>

	</view>
</template>

<script>
	import wxUtil from '@/utils/wxApplet/wxUtil.js'
	import {
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				// 是否显示获取头像昵称按钮
				isShowUserInfo: true,
				avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
			}
		},
		computed: {
			...mapGetters([
				'userInfo'
			])
		},
		onLoad() {
			this.isShowUserInfo = JSON.stringify(this.userInfo) == "{}" ? true : false
		},
		methods: {
			// 获取昵称、头像等信息
			getUserProfile() {
				wxUtil.getUserProfile().then(res => {
					if (res) {
						this.isShowUserInfo = false;
					}
				})
			},
			// 手机号快捷登录
			getPhoneNumber(e) {
				wxUtil.getPhoneNumber(e).then(res => {
					if (res) {
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						})
						setTimeout(() => {
							this.noSignIn();
						}, 1300)
					}
				})
			},
			//更改头像
			onChooseAvatar(e) {
				const tempFilePath = e.detail.avatarUrl //上传的图片地址
				const maxSizeInBytes = 1024 * 1024 // 限制大小为1MB
				uni.getFileInfo({
					filePath: tempFilePath,
					success: res => {
						const fileSize = res.size
						if (fileSize > maxSizeInBytes) {
							//如果上传的图片大小超过1MB，进行提示
							uni.$showMsg('请上传小于1MB的图片')
							return
						}
						//图片大小符合，替换图片
						avatarUrl.value = tempFilePath
						//将更改的图片上传到服务器
						uni.uploadFile({
							url: 'http://...', //后端接口url
							filePath: avatarUrl.value,
							name: 'file',
							header: {
								Authorization: 'Bearer ' + token // 将 token 添加到请求的 header 中
							},
							success(res) {
								let fileRes = JSON.parse(res.data)
								uni.$showMsg(fileRes.message)
							}
						})
					}
				})
			},
			//获取微信昵称
			bindblur(e) {
				console.log("---newNickname---"+e.detail.value);

			}
		}
	}
</script>

<style lang="scss" scoped>
	.my-user-info {
		height: 100vh;
		background-color: #f4f4f4;

		.top-box {
			display: flex;
			flex-direction: column;
			align-items: center;

			.avatar {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50rpx;
				border: 2rpx solid white;
				box-shadow: 0 1rpx 5rpx black;
				padding: 0;

				.avatar-img {
					width: 100%;
					height: 100%;
				}
			}

			.nickname {
				color: white;
				margin-top: 20rpx;
				text-align: center;
				font-size: 30rpx;
				font-weight: bold;
			}
		}
	}

	.avatarUrl {
		width: 100px;
		height: 100px;
		border-radius: 50px;
		overflow: hidden;
		border: 4rpx solid #fff;
		box-shadow: 0rpx 0rpx 10rpx $u-light-color;

		&_img {
			width: 100px;
		}
	}

</style>